package com.xc.freeapp.account.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Collectdoctor;
import com.xc.freeapp.entity.Doctor;
import com.xc.freeapp.entity.Relation;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.CollectDoctorService;
import com.xc.freeapp.service.DoctorService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.TokenAnnotation;
@Controller
@RequestMapping("/v2/collectDoctor")
@Api(value = "/v2/collectDoctor",description="收藏医生")
public class CollectDoctorControllerV2 extends AbstractController {
	@Autowired
	private CollectDoctorService collectDoctorServiceImpl;
	@Autowired
	private DoctorService doctorServiceImpl;
//	/**
//	 * 收藏专家
//	 * 
//	 * @param request
//	 * @return
//	 */
//	@TokenAnnotation(required=true)
//	@ApiOperation(value = "收藏专家")
//	@RequestMapping(value = "/collectDoctor",method = RequestMethod.POST)
//	@ResponseBody
	public ResponseBean collectDoctor(@RequestBody Collectdoctor collectdoctor)
			throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("userId", authInfo.getUserId());
			map.put("doctorid", collectdoctor.getDoctorid().toString());
			collectDoctorServiceImpl.collectDoctor(map);
			return ResponseBean.getSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(authInfo.getUserId() + "收藏失败");
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}

//	/**
//	 * 取消收藏接口
//	 * 
//	 * @param requestMap
//	 * @return
//	 * @throws BaseException
//	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "/collectDoctor",method = RequestMethod.DELETE)
//	@ResponseBody
//	@ApiOperation(value = "取消收藏接口")
	public ResponseBean delCollectDoctor(
			@RequestParam("doctorid") String doctorid)
			throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
			
			map.put("userid", authInfo.getUserId());
			map.put("doctorid", doctorid);
			collectDoctorServiceImpl.delCollectDoctor(map);
			return ResponseBean.getSuccess();
		} catch (Exception e) {
			log.error(authInfo.getUserId() + "取消收藏失败");
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}

	/**
	 * 获取收藏专家
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/collectDoctor",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取收藏专家")
	public ResponsePage<List<Doctor>> getCollectDoctor(
			@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		
		List<Doctor> collectdoctors = null;
		String userId = "";
		try {
			PageBounds pageBounds = new PageBounds(page, limit);
			collectdoctors = doctorServiceImpl.getCollectDoctor(authInfo.getUserId(),pageBounds);
			Paginator paginator =((PageList)collectdoctors).getPaginator();
			return new ResponsePage<List<Doctor>>(collectdoctors, paginator);
		} catch (Exception e) {
			log.error(userId + "查询收藏失败");
			 throw new BaseException(Constant.GLOBAL_ERROR); 
		}
	}

}
